package com.lagou.controller;

import com.lagou.pojo.User;
import com.lagou.service.CodeService;
import com.lagou.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Reference
    private CodeService codeService;

    /**
     * 注册接⼝，true成功，false失败
     * @param email
     * @param password
     * @param code
     * @return
     */
    @PostMapping("/register/{email}/{password}/{code}")
    public Map<String,Object> register(@PathVariable String email, @PathVariable String password, @PathVariable String code, HttpServletResponse response) {

        Map<String,Object> result = new HashMap<String, Object>();

        //0/1/2 校验验证码是否正确0正确1错误2超时
        Integer integer = codeService.validateCode(email, code);

        if (integer.equals(1)) {//验证码错误

            result.put("status",false);
            result.put("msg","验证码错误");

            return result;

        }else if(integer.equals(2)) {//验证码超时

            result.put("status",false);
            result.put("msg","验证码超时");

            return result;
        }

        Boolean registered = isRegistered(email);
        if(registered){

            result.put("status",false);
            result.put("msg","email已注册，请勿重复注册");

            return result;
        }


        userService.addUser(email,password);

        String token = UUID.randomUUID().toString().replaceAll("-","");
        userService.addToken(email,token);

        Cookie cookie=new Cookie("token",token);
        cookie.setDomain("localhost");
        cookie.setPath("/");
        response.addCookie(cookie);

        result.put("status",true);
        result.put("msg","注册成功");

        return result;
    }

    /**
     * 是否已注册，根据邮箱判断,true代表已经注册过，false代表尚未注册
     * @param email
     * @return
     */
    @GetMapping("/isRegistered/{email}")
    public Boolean isRegistered(@PathVariable String email) {

        User user = userService.findUser(email);

        if(user != null){
            return true;
        }

        return false;
    }

    /**
     * 登录接⼝，验证⽤户名密码合法性，根据⽤户名和密码⽣成token，token存⼊数\据库，并写⼊cookie中，登录成功返回邮箱地址，重定向到欢迎⻚
     * @param email
     * @param password
     * @return email
     */
    @GetMapping("/login/{email}/{password}")
    public Map<String,Object> login(@PathVariable String email, @PathVariable String password, HttpServletResponse response) {

        Map<String,Object> result = new HashMap<String, Object>();

        Boolean aBoolean = userService.validateUser(email, password);

        if(!aBoolean) {

            result.put("status",false);
            result.put("msg","用户名或密码错误");

            return result;
        }

        String token = UUID.randomUUID().toString().replaceAll("-","");
        userService.addToken(email,token);

        Cookie cookie=new Cookie("token",token);
        cookie.setDomain("localhost");
        cookie.setPath("/");
        response.addCookie(cookie);

        result.put("status",true);
        result.put("data",email);

        return result;
    }

    /**
     * 根据token查询⽤户登录邮箱接⼝
     * @param token
     * @return email
     */
    @GetMapping("/info/{token}")
    public Map<String,Object> infoToken(@PathVariable String token) {

        Map<String,Object> result = new HashMap<String, Object>();
        String email = userService.fingEmailByToken(token);
        result.put("status",true);
        result.put("data",email);
        return result;
    }


}
